package com.haipumi.biz.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.haipumi.beans.haipumi_product;
import com.haipumi.biz.BackGoodsBiz;
import com.haipumi.dao.DBHelper;
import com.haipumi.dao.IBackGoodDao;
import com.haipumi.mapper.BackGoodsMapper;


@Service("backGoodsBiz")
public class BackGoodsBizImpl implements BackGoodsBiz {
	
	@Autowired
	private BackGoodsMapper backGoodsMapper;
	
	@Override
	public List<haipumi_product> findAllGoods(int start,int end ,String sort,String order) {
		Map<String ,Object> map=new HashMap<String ,Object> ();
		map.put("start", start);
		map.put("end", end);
		map.put("order", order);
		map.put("sort", sort);
		return backGoodsMapper.findAllGood(map);
	}
	@Override
	public int findGoodsCount(int start,int end ,String sort,String order,String pname) {
		Map<String ,Object> map=new HashMap<String ,Object> ();
		map.put("start", start);
		map.put("end", end);
		map.put("order", order);
		map.put("sort", sort);
		if(pname==null||"".equals(pname)){
			map.put("pname",null);
			return backGoodsMapper.countAllGood( map);
		}else{
			map.put("pname", pname);
			return backGoodsMapper.countAllGood(map);
		}
	}
	@Override
	public haipumi_product addGoods(haipumi_product goods) {
		Map<String ,Object> map=new HashMap<String,Object>();
		map.put("pname", goods.getPname());
		map.put("pnum", Integer.parseInt(goods.getPnum()));
		map.put("pneed",Integer.parseInt(goods.getPneed()));
		map.put("pdiscount",Double.parseDouble(goods.getPdiscount()));
		map.put("pphoto", goods.getPphoto());
		int r=backGoodsMapper.addGood(map);;
		if(r>0){
			return goods;
		}
		return null; 
	}
	@Override
	public haipumi_product updateGoods(haipumi_product goods) {
		Map<String ,Object> map=new HashMap<String,Object>();
		map.put("pname", goods.getPname());
		map.put("pnum", Integer.parseInt(goods.getPnum()));
		map.put("pneed",Integer.parseInt(goods.getPneed()));
		map.put("pdiscount",Double.parseDouble(goods.getPdiscount()));
		map.put("pid", Integer.parseInt(goods.getPid()));
		int r=backGoodsMapper.updateGood(map);
		if(r>0){
			return goods;
		}
		return null;
	}
	@Override
	public List<haipumi_product> findGoods(String pname, int rows, int page,
			String sort, String order) {
		return null;
	}
	@Override
	public haipumi_product updateGoodsStatus(haipumi_product goods) {
		Map<String ,Object> map=new HashMap<String,Object>();
		map.put("pstatus", Integer.parseInt(goods.getPstatus()));
		map.put("pid",Integer.parseInt( goods.getPid()));
		int r=backGoodsMapper.updateGoodsStatus(map);
		if(r>0){
			return goods;
		}
		return null;
	}

	@Override
	public boolean updateGoodsPic(haipumi_product goods) {
		Map<String ,Object> map=new HashMap<String,Object>();
		map.put("pphoto", goods.getPphoto());
		int r=backGoodsMapper.updateGoodsPic(map);
		if(r>0){
			return true;
		}
		return false;
	}
	@Override
	public haipumi_product updateGoodsDetail(haipumi_product goods) {
		Map<String ,Object> map=new HashMap<String,Object>();
		map.put("pdescribe", goods.getPdescribe());
		int r=backGoodsMapper.updateGoodsDetail(map);
		if(r>0){
			return goods;
		}
		return null;
	}
}
